<?php
error_reporting(E_ALL ^ E_NOTICE);

use Websocket\WebsocketClient;

require_once './Websocket/WebsocketClient.php';
require_once './Websocket/WebsocketConnection.php';
require_once './Net/Event.php';

$client = new WebsocketClient();

//则塞模式
$client->setBlock(true);

$client->connect('127.0.0.1', 30002);

if($client->handshake) {
    echo "连接成功!\n";
    $client->sendMessage('hello hello hello');
    sleep(1);
    $client->sendMessage('are you ok');
    sleep(1);
    
    $message = $client->recvMessage();
    echo "服务器回复: $message\n";
    
    echo "继续发送\n";
    $client->sendMessage('hello');
    sleep(1);
    $client->sendMessage('are you ok');
    sleep(3);
    
    $buffer = $client->recvMessage(true);
    $messages = $buffer->fetchAllMessages();
    echo "收到的服务器回复: " . implode(' ', $messages) . "\n";
    
    sleep(3);
    $client->sendMessage('exit');
} else {
    echo "连接错误: {$client->getLastError()} {$client->getLastErrorMessage()}\n";
}




